import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:kxy_flutter_app/auth.dart';
import 'package:kxy_flutter_app/components/loading_dialog.dart';
import 'package:kxy_flutter_app/components/loading_widget.dart';
import 'package:kxy_flutter_app/helper/http.dart';
import 'package:kxy_flutter_app/model/exam_question.dart';
import 'package:kxy_flutter_app/view/examination/do_review_view.dart';

class MistakeListView extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MistakeListViewState();
  }
}

class MistakeListViewState extends State<MistakeListView> {
  final int limit = 20;
  Future loadFuture;
  int page = 1;
  int totalCount = 0;
  int swiperIndex = 0;
  List<Question> quesList = new List<Question>();
  SwiperController swiperCtrl = new SwiperController();
  LoadingDialog loading;

  @override
  void initState() {
    loading = LoadingDialog(context);
    loadFuture = loadQuesList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      future: loadFuture,
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return body();
        }
        return LoadingWidget();
      },
    );
  }

  Widget body() {
    return DoReviewView(
      title: '我的错题',
      quesList: quesList,
      readonly: false,
      totalCount: totalCount,
      loadMore: loadQuesList,
      onChange: (index) => swiperIndex = index,
      withCollect: false,
      actions: [
        FlatButton(
          padding: EdgeInsets.all(0),
          child: Text('移除'),
          onPressed: removeCurrent,
        ),
      ],
    );
  }

  Future reload() async {
    page = 1;
    totalCount = 0;
    quesList.clear();
    await loadQuesList();
  }

  Future loadQuesList() async {
    var res = await Http.instance.post('/exam-app/error/getQuesErrorList', params: {
      'userId': Auth.instance.userId,
      'page': page,
      'limit': limit,
    });

    if (res.code == 200) {
      if (!mounted) return;

      setState(() {
        totalCount = res.data['totalCount'];
        (res.data['list'] as List).forEach((item) {
          var ques = Question.fromJson(item);
          if (quesList.indexWhere((x) => x.quesId == ques.quesId) == -1) {
            quesList.add(ques);
          }
        });
        page++;
      });
    } else {
      Fluttertoast.showToast(msg: res.msg);
    }
  }

  Future removeCurrent() async {
    if (quesList.length == 0) return;
    loading.show();
    var res = await Http.instance.post('/exam-app/error/removeQuesError', params: {
      'quesId': quesList[swiperIndex].quesId,
      'userId': Auth.instance.userId,
    });
    loading.dismiss();
    if (res.code == 200) {
      if (quesList.length < swiperIndex + 5 && quesList.length < totalCount) {
        page = (quesList.length / limit).ceil();
        await loadQuesList();
      }
      setState(() {
        quesList.removeAt(swiperIndex);
        totalCount--;
      });
    } else {
      Fluttertoast.showToast(msg: res.msg);
    }
  }
}
